/*
 * Copyright 2008 Zoundry LLC, Inc.  All Rights Reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 */
package org.zoundry.gaedt.ui.views.actions;

import java.net.URL;
import java.text.MessageFormat;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.browser.IWebBrowser;
import org.eclipse.ui.browser.IWorkbenchBrowserSupport;
import org.eclipse.ui.internal.browser.WorkbenchBrowserSupport;
import org.zoundry.gaedt.core.util.ZGaeUtil;
import org.zoundry.gaedt.services.applications.IZGaeApplication;
import org.zoundry.gaedt.services.applications.TZGaeApplicationStatus;
import org.zoundry.gaedt.services.applications.ZGaeApplicationsService;


/**
 * This action will open the selected {@link IZGaeApplication}, if it is running,
 * in an embedded web browser.
 */
@SuppressWarnings("restriction")
public class ZGaeBrowseApplicationAction extends ZGaeApplicationsViewActionBase
{
    /**
     * C'tor.
     *
     * @param aViewer
     * @param aService
     */
    public ZGaeBrowseApplicationAction(TableViewer aViewer, ZGaeApplicationsService aService)
    {
        super("Browse...", "If running, browses to the application in an embedded web browser.", null, aViewer, aService);
    }

    /**
     * @see org.eclipse.jface.action.Action#run()
     */
	@Override
    public void run()
    {
        IZGaeApplication application = getSelectedApplication();
        if (application.getStatus() == TZGaeApplicationStatus.STOPPED)
        {
            Shell shell = Display.getCurrent().getActiveShell();
            String message = MessageFormat.format("The application \"{0}\" is not currently running.",
                    application.getModel().getName());
            MessageDialog.openInformation(shell, "Application Not Running", message);
        }
        else
        {
            try
            {
                IWorkbenchBrowserSupport support = WorkbenchBrowserSupport.getInstance();

                int style = IWorkbenchBrowserSupport.AS_EDITOR | IWorkbenchBrowserSupport.NAVIGATION_BAR | IWorkbenchBrowserSupport.STATUS;

                String browserId = "_gaeBrowser_" + application.getModel().getName();
                String browserName = "Browse: " + application.getModel().getName();
                String browserTooltip = "Browser for GAE application: " + application.getModel().getName();
                String urlString = MessageFormat.format("http://localhost:{0,number,#}", application.getModel().getDevPort());
                URL url = new URL(urlString);
                IWebBrowser browser = support.createBrowser(style, browserId, browserName, browserTooltip);
                browser.openURL(url);
            }
            catch (Exception e)
            {
                ZGaeUtil.logAndDisplayError(e);
            }
        }
    }
}
